using System;
using System.Collections.Generic;
using System.Text;
using DoAn_LyThuyet.HeThongXuLy;
using System.Data;

namespace DoAn_LyThuyet.HeThongLuuTru
{
    class TiepNhanTaiSanDAO
    {
        //----------------LOAI TAI SAN-----------------
        //lay ra danh sach tai san.
        static public DataTable SelectTLAll()
        {
            string sql = "select * from LoaiTaiSan";
            DataTable dt = DataAccess.GetTable(sql);
            return dt;
        }
        // them du lieu.
        static public void Insert(TiepNhanTaiSanDTO lts)
        {
            string sql = "insert into LoaiTaiSan(MaLoaiTaiSan, TenLoaiTaiSan) values('"  + lts.MaLoai + "','" + lts.TenLoai + "')";

            DataAccess.GetTable(sql);
        }
        //xoa du lieu
        static public void Delete(TiepNhanTaiSanDTO lts)
        {
            string sql = "Delete from LoaiTaiSan where MaLoaiTaiSan = '" + lts.MaLoai + "'";
            DataAccess.GetTable(sql);

        }
        // lay ra loai dua vao ten
        static public TiepNhanTaiSanDTO SelectTLByName(string name)
        {
            string sql = "select * from LoaiTaiSan where TenLoaiTaiSan = '" + name + "'";
            DataTable dt = DataAccess.GetTable(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                TiepNhanTaiSanDTO lts = new TiepNhanTaiSanDTO();
                lts.MaLoai = dt.Rows[0].ItemArray[0].ToString();
                lts.TenLoai = dt.Rows[0].ItemArray[1].ToString();
                return lts;
            }
        }
        static public TiepNhanTaiSanDTO SelectTLByCode(string name)
        {
            string sql = "select * from LoaiTaiSan where MaLoaiTaiSan = '" + name + "'";
            DataTable dt = DataAccess.GetTable(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                TiepNhanTaiSanDTO lts = new TiepNhanTaiSanDTO();
                lts.MaLoai = dt.Rows[0].ItemArray[0].ToString();
                lts.TenLoai = dt.Rows[0].ItemArray[1].ToString();
                return lts;
            }
        }
        public static bool SuaLoai(TiepNhanTaiSanDTO lts)
        {
            try
            {
                string sql = "Update LoaiTaiSan set TenLoaiTaiSan ='" + lts.TenLoai + "' where MaLoaiTaiSan = '" + lts.MaLoai+"'";
                         // "insert into LoaiTaiSan(MaLoaiTaiSan, TenLoaiTaiSan) values('"  + lts.MaLoai + "','" + lts.TenLoai + "')"
                DataAccess.GetTable(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        
        //------------------Xu ly tai san-------------------
        static public DataTable SelectTaiSanAll()
        {
            string sql = "select * from TaiSan";

            DataTable dt = DataAccess.GetTable(sql);
            return dt;
        }

        static public DataTable SelectTaiSanAll_theoloaits(String pmaloaitaisan)
        {
            string sql = "select * from TaiSan Where  MaLoaiTaiSan = '" + pmaloaitaisan + "'";

            DataTable dt = DataAccess.GetTable(sql);
            return dt;
        }

        static public void Insertts(TiepNhanTaiSanDTO s)
        {
            string sql = "insert into TaiSan(MaTaiSan,TenTaiSan,XuatSu,NamSanXuat,MaLoaiTaiSan) values('" + s.MaTaiSan+ "','" +
                s.TenTaiSan + "','" + s.XuatSu + "'," + s.NamSanXuat+ ",'" + s.MaloaiTaiSan+ "')";
            DataAccess.GetTable(sql);
        }

        public static bool SuaTaiSan(TiepNhanTaiSanDTO ts)
        {
            try
            {
                string sql = "Update TaiSan set TenTaiSan = '" + 
                    ts.TenTaiSan + "',XuatSu = '" +
                    ts.XuatSu+"', NamSanXuat = " +
                    ts.NamSanXuat +", MaLoaiTaiSan = '"+
                    ts.MaloaiTaiSan +"' where MaTaiSan ='" + ts.MaTaiSan+"'";
                DataAccess.GetTable(sql);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        static public void Deletets(TiepNhanTaiSanDTO ts)
        {
            string sql = "Delete from TaiSan where MaTaiSan = '" + ts.MaTaiSan + "'";
            DataAccess.GetTable(sql);
        }

        static public TiepNhanTaiSanDTO SelectTS(string name)
        {
            string sql = "select * from TaiSan where MaTaiSan = '" + name + "'";
            DataTable dt = DataAccess.GetTable(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                TiepNhanTaiSanDTO ts = new TiepNhanTaiSanDTO();
                ts.MaTaiSan = dt.Rows[0].ItemArray[0].ToString();
                ts.TenTaiSan = dt.Rows[0].ItemArray[1].ToString();
                ts.XuatSu = dt.Rows[0].ItemArray[2].ToString();
               // ts.NamSanXuat = dt.Rows[0].ItemArray[3].ToString();
                return ts;
            }
        }
        
    }
}
